package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.Constant;
import com.gzw.dms.bean.SingnInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.HashMap;
import java.util.Map;

public class MySelfActivity extends BaseActivity {
    private Button btn_phone, btn_exit;
    private Context mContext;
    private TextView tv_user, tv_customer, tv_username,tv_name,tv_phone,tv_password,
            tv_foodback;
    private EditText ed_oldpassword,ed_newpassword,ed_sure,ed_phone;
    private String apkVersion;
    private String tag = "SetActivity----->";
    private Intent mIntent;
    private Toolbar toolbar;
    private Boolean cloose = true;
    private Boolean cloose2 = true;

    private LinearLayout layout_look,layout_phone;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_my_self);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        btn_exit = (Button) findViewById(R.id.btn_exit);
        tv_user = (TextView) findViewById(R.id.tv_user);
        tv_customer = (TextView) findViewById(R.id.tv_customer);
        tv_username = (TextView) findViewById(R.id.tv_username);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_password = (TextView) findViewById(R.id.tv_password);
        ed_oldpassword = (EditText) findViewById(R.id.ed_oldpassword);
        ed_newpassword = (EditText) findViewById(R.id.ed_newpassword);
        ed_sure = (EditText) findViewById(R.id.ed_sure);
        layout_look = (LinearLayout) findViewById(R.id.layout_look);
        layout_look.setVisibility(View.GONE);
        ed_sure.setTransformationMethod(PasswordTransformationMethod.getInstance());
        ed_oldpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        ed_newpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        ed_phone = (EditText) findViewById(R.id.ed_phone);
        layout_phone = (LinearLayout) findViewById(R.id.layout_phone);
        layout_phone.setVisibility(View.GONE);
        btn_phone = (Button) findViewById(R.id.btn_phone);

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("个人中心");
        mIntent = new Intent();
        UserInfo userInfo = MyApplication.getUserInfo();
        String flag = userInfo.getFlag();
        if (flag.equals("0")){
            tv_user.setText("超级管理员");
        }else  if (flag.equals("1")){
            tv_user.setText("司机");
        }else  if (flag.equals("2")){
            tv_user.setText("客户");
        }else  if (flag.equals("3")){
            tv_user.setText("省内项目部");
        }else  if (flag.equals("4")){
            tv_user.setText("省外项目部");
        }
        tv_customer.setText(userInfo.getDepartment_name());
        tv_username.setText(userInfo.getUsername());
        tv_name.setText(userInfo.getRealName());
        tv_phone.setText(userInfo.getUserPhone());

    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new MyOnClick());
        btn_exit.setOnClickListener(new MyOnClick());
        tv_password.setOnClickListener(new MyOnClick());
        btn_phone.setOnClickListener(new MyOnClick());
        tv_phone.setOnClickListener(new MyOnClick());
    }

    class MyOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences sharedPreferences = getSharedPreferences(
                    "UserInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            switch (v.getId()) {

                case R.id.tv_password:
                    if (cloose==false){
                        layout_look.setVisibility(View.GONE);
                        cloose= !cloose;
                    }else if (cloose==true){
                        layout_look.setVisibility(View.VISIBLE);
                        cloose= !cloose;

                    }
                    break;
                case R.id.tv_phone:
                    if (cloose2==false){
                        layout_phone.setVisibility(View.GONE);
                        cloose2= !cloose2;
                    }else if (cloose2==true){
                        layout_phone.setVisibility(View.VISIBLE);
                        cloose2= !cloose2;
                    }
                    break;
                case R.id.btn_exit:
                    readdata();
                    break;
                case R.id.btn_phone:
                    updatePhone();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }
    protected  void readdata(){

        if (isNull(ed_oldpassword)) {
            T.showShort(mContext, "旧密码不能为空");
            return;
        }
        if (isNull(ed_newpassword)) {
            T.showShort(mContext, "新密码不能为空");
            return;
        }
        if (isNull(ed_sure)) {
            T.showShort(mContext, "确认密码不能为空");
            return;
        }
        String oldpass = ed_oldpassword.getText().toString().trim();
        String newpass = ed_newpassword.getText().toString().trim();
        String surepass = ed_sure.getText().toString().trim();
        if (newpass.equals(surepass)){
            SignShipOrder signShipOrder = new SignShipOrder();
            signShipOrder.execute(oldpass, newpass);
        }else {
            T.showShort(mContext, "两次输入密码不一致！");
            return;
        }

    }
    public class SignShipOrder extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("id", userInfo.getId());
            map.put("password1", params[0]);
            map.put("password2", params[1]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appModifyPassword"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putBoolean(Constant.IS_USER_LOGIN_KEY, false);
                    edit.commit();
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(2, mIntent);
                    finish();

                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }

    private void updatePhone(){
        if (isNull(ed_phone)) {
            T.showShort(mContext, "新手机号不能为空");
            return;
        }

            String phoneNum = ed_phone.getText().toString().trim();

            UpdatePhone updatePhone = new UpdatePhone();
            updatePhone.execute(phoneNum);

    }

    public class UpdatePhone extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("phone", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appUpdatePersonInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    UserInfo userInfo = MyApplication.getUserInfo();
                    userInfo.setUserPhone(ed_phone.getText().toString().trim());
                    cloose2 = false;
                    layout_phone.setVisibility(View.GONE);
                    tv_phone.setText(userInfo.getUserPhone());
                    SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("userPhone",userInfo.getUserPhone());
                    edit.commit();

                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }
}
